<?php

# 
# Copyright (c) 2002-2007 Eric Wallengren
# This file is part of the Continuous Automated Build and Integration 
# Environment (CABIE)
# 
# CABIE is distributed under the terms of the GNU General Public
# License version 2 or any later version.  See the file COPYING for copying 
# permission or http://www.gnu.org. 
#                                                                            
# THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED OR  
# IMPLIED, without even the implied warranty of MERCHANTABILITY or FITNESS 
# FOR A PARTICULAR PURPOSE.  ANY USE IS AT YOUR OWN RISK. 
#                                                                            
# Permission to modify the code and to distribute modified code is granted, 
# provided the above notices are retained, and a notice that the code was 
# modified is included with the above copyright notice. 
# 

    #
    # Load required files
    #
    require_once("defs.php");
    require_once("dbdefs.php");

    #
    # Declare global for use with configuration table
    #
    global $servers;

    #
    # Create the array to hold the list of servers we'll use
    #
    $servers = array();

    $SQL = new sqlfuncs();
    $SQL->__construct($server, $username, $password, $database);

    #
    # Grab all active servers
    #
    $sqlarray = _getactiveservers($SQL, 0);

    #
    # Assign servers to global var 
    #
    foreach ($sqlarray as $item) {
        $servers[$item] = 1;
    }

    #
    # Queries used for grabbing data for each named table
    #
    $queries = array(

        #
        # Search is used by the editor to find a record
        #
        # Condition is used after submitting the edited record back to the
        # table.
        #
        # Each table used by the editor requires a search and condition
        # to operate correctly in the switch statement in editor.php
        #  See 'userpass' in the users array towards the bottom of this file
        #
        "configuration"     => array(
            "search"    => "select * from %s where server='%s' and title=".
                           "'%s'",
            "condition" => "where server='%s' and title='%s'"
        ),

        "testconfiguration" => array(
            "search"    => "select * from %s where buildjob='%s' and title=".
                           "'%s'",
            "condition" => "where buildjob='%s' and title='%s'"
        ),

        "users"             => array(
            "search"    => "select * from %s where username='%s'",
            "condition" => "where username='%s'"
        ),

        "comments"          => array(
            "search"    => "select comments.*, jobs.status from %s, jobs ".
                           "where comments.job = jobs.job and ".
                           "comments.server='%s' and comments.title='%s' ".
                           "and comments.job='%s'",
            "newsearch" => "select status from jobs where server='%s' and ".
                           "title='%s' and job='%s'",
            "subsearch" => "select * from %s where server='%s' and title=".
                           "'%s' and job='%s'",
            "condition" => "where server='%s' and title='%s' and job='%s'",
            "statup"    => "update jobs set status='%s' where server='%s' ".
                           "and title='%s' and job='%s'"
        ),

    );

    #
    # Access levels for each named tables
    #
    $access = array(

        #
        # Define levesl for editing tables in the builds database.
        # adminlevel provides full access userlevel provides limited
        # functionality on a case by case basis....
        #
        "configuration"     => array(
            "adminlevel" => 99,
            "userlevel"  => 99
        ),

        "testconfiguration" => array(
            "adminlevel" => 99,
            "userlevel"  => 99
        ),

        "users"     => array(
            "adminlevel" => 99,
            "userlevel"  => 1
        ),

        "comments"     => array(
            "adminlevel" => 99,
            "userlevel"  => 1
        ),

    );

    #
    # Manus to show for each named table
    #
    $menus = array(

        #
        # There are 2 main menus, the server menu and the job menu.
        # When in the editor, the appropriate menu can be displayed 
        # by setting it (1) on, or (0) off.
        #
        "configuration"     => array(
            "server" => 1,
            "job"    => 1
        ),

        "testconfiguration" => array(
            "server" => 1,
            "job"    => 1
        ),

        "users"     => array(
            "server" => 1,
            "job"    => 0
        ),

        "comments"     => array(
            "server" => 1,
            "job"    => 1
        ),

    );

    // Define DB table descriptions for editing...

    $tables = array(

        #
        # Charastics to be displayed for each table when in the editor.
        #
        # Table
        #    Field
        #        description: help with the entry
        #        type: input, option, textarea, textfield, password
        #        editable: 1 yes, 0 no
        #        size: the size of the field
        #        (selection for option)
        #        level: access level for editing the individual field
        #        validate: field validation to be fed through 
        #                  gen_validatorv2.js
        #        size: the size of the field (don't exceed that of the table)
        #        
        # A special field type called 'validate' exists for passwords
        # and is handled differently by the submit function in editor.php
        #
        "configuration" => array(

            "server"      => array(
                "description" => "The name of the buildserver the %s job is ".
                                 "running on.",
                "editable"    => 1,
                "type"        => "option",
                "size"        => count($servers),
                "selection"   => $servers,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires more than ".
                                   "one character",
                    "maxlen=30" => "this field may not exceed 30 ".
                                   "characters",
                    ),
                "size"        => 30
            ),

            "title"       => array(
                "description" => "The name of the job running on the ".
                                 "buildserver",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires more than ".
                                   "one character",
                    "maxlen=30" => "this field may not exceed 30 ".
                                   "characters",
                    ),
                "size"        => 30
            ),
    
            "port"        => array(
                "description" => "Perforce port, CVSROOT or SVNROOT for the ".
                                 "%s job",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires more than ".
                                   "one character",
                    "maxlen=30" => "this field may not exceed 30 ".
                                   "characters",
                    ),
                "size"        => 30
            ),
    
            "client"      => array(
                "description" => "Perforce client name, CVS module list or ".
                                 "SVN directory list",
                "type"        => "textarea",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires more than ".
                                     "one character",
                    "maxlen=1024" => "this field may not exceed 30 ".
                                     "characters",
                    ),
                "rows"        => 1,
                "size"        => 1024
            ),
    
            "top"         => array(
                "description" => "The toplevel directory where code from ".
                                 "Perforce, CVS or SVN is checked out ",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"   => "this field cannot be left blank",
                    "minlen=2"   => "this field requires more than ".
                                    "one character",
                    "maxlen=255" => "this field may not exceed 30 ".
                                    "characters",
                    ),
                "size"        => 255
            ),
    
            "type"        => array(
                "description" => "Type of build, Retail (no debug symbols), ".
                                 "debug (debug symbols), or both. User must ".
                                 "supply appropriate scripts for build types.",
                "type"        => "option",
                "size"        => 3,
                "editable"    => 1,
                "level"       => 99,
                "selection"   => array(
                                       "retail" => "default",
                                       "debug"  => "",
                                       "both"   => ""
                )
            ),
    
            "top"         => array(
                "description" => "The toplevel directory where code from ".
                                 "Perforce, CVS or SVN is checked out",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"   => "this field cannot be left blank",
                    "minlen=2"   => "this field requires more than ".
                                    "one character",
                    "maxlen=255" => "this field may not exceed 30 ".
                                    "characters",
                    ),
                "size"        => 255
            ),
    
            "toolsdir"    => array(
                "description" => "Directories separated by OS specific path ".
                                 "seperators pointing to binary directories",
                "type"        => "textarea",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires more than ".
                                     "one character",
                    "maxlen=1024" => "this field may not exceed 30 ".
                                     "characters",
                    ),
                "rows"        => 1,
                "size"        => 1024
            ),
    
            "isimake"     => array(
                "description" => "Use imake: deprecated, any change here will ".
                                 "not affect the operation of this job",
                "type"        => "option",
                "size"        => 2,
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "selection"   => array(
                                       "yes" => "",
                                       "no"  => "default"
                )
            ),
    
            "keeplevel"   => array(
                "description" => "The number of builds to keep before ".
                                 "reclaiming older builds",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required" => "this field cannot be left blank",
                    "minlen=1" => "this field requires at least one digit ",
                    "maxlen=4" => "this field may not exceed 4 digits",
                    "num"      => "this field must be numeric",
                    ),
                "size"        => 4
            ),
    
            "comment"     => array(
                "description" => "A discription for the %s job that can be ".
                                 "displayed via the web or from the build ".
                                 "client",
                "type"        => "textarea",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires more than ".
                                     "one character",
                    "maxlen=1024" => "this field may not exceed 30 ".
                                     "characters",
                    ),
                "rows"        => 1,
                "size"        => 1024
            ),
    
            "sccs"        => array(
                "description" => "The CM engine used for the % job. Currently ".
                                 "only Perforce, CVS and SVN are supported",
                "type"        => "option",
                "size"        => 3,
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "selection"   => array(
                                       "perforce"   => "default",
                                       "cvs"        => "",
                                       "subversion" => ""
                )
            ),
    
            "browserlink" => array(
                "description" => "A URL used to point a file and it's ".
                                 "revision off to a web browser for viewing. ".
                                 "Two args passed as '&#37;s' (for CVS and ".
                                 "Subversion) maybe used and will be filled ".
                                 "out automatically in the order of filename ".
                                 "then revision. For perforce a single ".
                                 "'&#37;s' is used to show the changelist ".
                                 "number",
                "type"        => "textarea",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires more than ".
                                     "one character",
                    "maxlen=1024" => "this field may not exceed 30 ".
                                     "characters",
                    ),
                "rows"        => 1,
                "size"        => 1024
            ),
    
            "state"       => array(
                "description" => "A value used to indicate to the server if ".
                                 "the %s job is active or not.",
                "type"        => "option",
                "size"        => 2,
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "selection"   => array(
                                       "yes"  => "default",
                                       "no" => ""
                ),
                "bool"        => array(
                                       0     => "yes",
                                       1     => "no"
                )
            ),
    
            "spam"        => array(
                "description" => "A value used to indicate whether email ".
                                 "should be sent to a globally defined group ".
                                 "for build failures",
                "type"        => "option",
                "size"        => 2,
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "selection"   => array(
                                       "yes"  => "",
                                       "no"   => "default"
                ),
                "bool"        => array(
                                       0     => "no",
                                       1     => "yes"
                )
            ),
    
            "buildsize"   => array(
                "description" => "Used by CABIE to store the size of the last ".
                                 "successful build.  Set to 0 for new jobs",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=1"  => "this field requires at least one digit ",
                    "maxlen=64" => "this field may not exceed 64 digits",
                    "num"       => "this field must be numeric",
                    ),
                "size"        => 64
            )
    
        ),

        "testconfiguration" => array(

            "server"        => array(
                "description" => "The name of the server the UI tests will ".
                                 "be executed on",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "title"        => array(
                "description" => "The title of the smoke test job",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "type"         => array(
                "description" => "The 'type' of installation to be performed ".
                                 "represented as an integer.  This will be ".
                                 "matched to what is set as TYPE in a POSIX ".
                                 "environment or ITYPE in Windows",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=3"  => "this field may not exceed 3 digits",
                    "num"       => "this field must be numeric",
                    ),
                "size"        => 3
            ),

            "imagename"    => array(
                "description" => "The name of the raw OS image deployed onto ".
                                 "the smoketest server.  With VMWare it is ".
                                 "the name of the vmx file used to launch ".
                                 "the VM.  With jumpstart it is the name of ".
                                 "the  jumpstart image",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=64" => "this field may not exceed 64 characters",
                    ),
                "size"        => 64
            ),

            "bootscript"    => array(
                "description" => "The name of the configuration script used ".
                                 "on a freshly deployed OS image. This must ".
                                 "be a script compatible with the NSI binary",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=64" => "this field may not exceed 64 characters",
                    ),
                "size"        => 64
            ),

            "comment"       => array(
                "description" => "Comment used to describe this smoke test job",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires at least one digit ",
                    "maxlen=1024" => "this field may not exceed 1024 ".
                                     "characters",
                    ),
                "size"        => 1024
            ),

            "state"        => array(
                "description" => "A value used to indicate whether the ".
                                 "smoke test is active or not.",
                "type"        => "option",
                "size"        => 2,
                "editable"    => 1,
                "level"       => 99,
                "selection"   => array(
                                       "yes"  => "default",
                                       "no" => ""
                ),
                "bool"        => array(
                                       0     => "yes",
                                       1     => "no"
                )
            ),

            "nextjob"      => array(
                "description" => "Unused at this time",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "spam"         => array(
                "description" => "Unused at this time",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 30
            ),

            "buildjob"      => array(
                "description" => "Buildserver name and title of job to be ".
                                 "tested in buildserver:jobname format",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "deployto"      => array(
                "description" => "The name of the machine the image will be ".
                                 "deployed to",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),
    
            "platform"      => array(
                "description" => "The target platform for which the image is ".
                                 "compatible (w2k, sol, linux, hpux)",
                "type"        => "option",
                "size"        => 4,
                "editable"    => 1,
                "level"       => 99,
                "selection"   => array(
                                       "linux" => "default",
                                       "w2k"   => "",
                                       "sol"   => "",
                                       "hpux"  => ""
                )
            ),

            "imageserver"   => array(
                "description" => "The server where a virtual image resides. ".
                                 "For windows it's the machine hosting the ".
                                 "VMWare image, for POSIX it's the name of ".
                                 "the jumpstart server where the jumpstart ".
                                 "configuration file exists",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "reference"     => array(
                "description" => "For POSIX the name of the configuration ".
                                 "used by jumpstart to deploy the image. ".
                                 "For Windows, the name of the directory ".
                                 "where the VMX (image) file exists",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "attempts"      => array(
                "description" => "The number of attempts that may be ".
                                 "required to get the image to be installed ".
                                 "successfully onto the raw OS image",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "validate"    => array( 
                    "required" => "this field cannot be left blank",
                    "minlen=2" => "this field requires at least one digit ",
                    "maxlen=3" => "this field may not exceed 3 digits",
                    "num"       => "this field must be numeric",
                    ),
                "size"        => 3
            ),

            "dependserver"  => array(
                "description" => "The name of another test server this ".
                                 "deployment may have to wait for",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "validate"    => array( 
                    "required"  => "this field cannot be left blank",
                    "minlen=2"  => "this field requires at least one digit ",
                    "maxlen=30" => "this field may not exceed 30 characters",
                    ),
                "size"        => 30
            ),

            "completed"     => array(
                "description" => "Unused at this time",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 2
            ),

        ),

        "users" => array(

            "userid"        => array(
                "description" => "The unigue ID of the user.  Automatically ".
                                 "generated when a new user is added",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 9
            ),

            "username"      => array(
                "description" => "The name the user will use to login",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 30
            ),

            "userpass"      => array(
                "description" => "The password the user will use to login",
                "type"        => "password",
                "editable"    => 1,
                "level"       => 1,
                "md5"         => 1,
                "validate"    => array( 
                    "required"    => "this field cannot be left blank",
                    "minlen=2"    => "this field requires at least one digit ",
                    "maxlen=32"   => "this field may not exceed 1024 ".
                                     "characters",
                    "pwmatch=v_verification"   
                                  => "password verification failed",
                    ),
                "help"        => "Required field",
                "size"        => 32
            ),

            "verification"  => array(
                "description" => "Verify password",
                "type"        => "verification",
                "editable"    => 1,
                "level"       => 1,
                "md5"         => 1,
                "help"        => "Required field",
                "size"        => 32
            ),

            "userrealm"     => array(
                "description" => "The identifier used to describe the ".
                                 "functionality the user will be allowed to ".
                                 "access",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 30
            ),

            "realmid"      => array(
                "description" => "The realmid associated with the users realm",
                "type"        => "input",
                "editable"    => 1,
                "level"       => 99,
                "help"        => "Required field",
                "size"        => 30
            ),

        ),

        "comments" => array(

            "server"      => array(
                "description" => "The name of the buildserver the %s job is ".
                                 "running on",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 1,
                "help"        => "Required field",
                "size"        => 30
            ),

            "title"      => array(
                "description" => "The name of the job running on the ".
                                 "buildserver",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 1,
                "help"        => "Required field",
                "size"        => 30
            ),

            "job"      => array(
                "description" => "The completed job identifier",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 1,
                "help"        => "Required field",
                "size"        => 25
            ),

            "comment"      => array(
                "description" => "Comment already associated with this job",
                "type"        => "textarea",
                "editable"    => 0,
                "level"       => 1,
                "help"        => "Required field",
                "rows"        => 8,
                "combine"     => "user",
                "size"        => 1024
            ),

            "user"      => array(
                "description" => "The name of the user modifying this ".
                                 "record",
                "type"        => "input",
                "editable"    => 0,
                "level"       => 1,
                "help"        => "Required field",
                "combine"     => "newcomment",
                "size"        => 30
            ),

            "newcomment"      => array(
                "description" => "New comments for this job",
                "type"        => "appendtextarea",
                "editable"    => 1,
                "level"       => 1,
                "help"        => "Required field",
                "rows"        => 8,
                "size"        => 2048
            ),
    
            "status"        => array(
                "description" => "Test status for this build",
                "type"        => "option",
                "size"        => 3,
                "editable"    => 1,
                "level"       => 1,
                "selection"   => array(
                                       "untested"    => "default",
                                       "passed test" => "",
                                       "failed test" => "",
                                       "failed unit" => ""
                )
            ),

        ),

    );
            
?>
